package com.yanqu.road.dataHelper.action.excel;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dataHelper.gameServer.GameServer;
import com.yanqu.road.dataHelper.manager.ActivityMgr;
import com.yanqu.road.dataHelper.manager.BaseMgr;
import com.yanqu.road.dataHelper.manager.DataMgr;
import com.yanqu.road.dataHelper.repair.BaseRepair;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.cookboy.process2.CookBoy2Dish;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CookBoy2 extends BaseRepair {

    public CookBoy2(GameServer gameServer) {
        super(gameServer);
    }

    @Override
    public void repair(long param1, String param2) {

    }

    public void goAllActivity(int activityId){
        go(activityId);
    }

    private void go(int activityId){
        List<Map<String, String>> listMap = new ArrayList<>();
        BaseMgr.getMgr(ActivityMgr.class).initActivity(activityId);
        ActivityInfo activityInfo = ActivityMgr.getActivityInfo(activityId);
        if(activityInfo == null){
            System.err.println("没有找到活动" + activityId);return;
        }
        if(!activityInfo.getServerIdList().contains(getGameServer().getServerInfo().getServerId())){
            System.err.println(activityId + "不是" + getGameServer().getServerInfo().getServerId()+"的活动");return;
        }
        listMap = getLog1(activityId);
        DataMgr.addData(listMap);
    }

    //触发礼包一条龙三个日志
    public void go1(){
        int activityId = 9975980;
        go(activityId);
    }
    //
    private List<Map<String, String>> getLog1(int activityId) {
        List<Map<String, String>> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "SELECT * FROM t_u_activity_cookboy WHERE no = 2 AND activityId = ?";
        try {
            call = getGameServer().getConnection().prepareCall(sql);
            int i=1;
            call.setInt(i++,activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                Map<String,String> temp = new ConcurrentHashMap<>();
                temp.put("serverId",String.valueOf(getGameServer().getServerInfo().getServerId()));
                temp.put("userId",checkNullToEmpty(rs.getString("userId")));
                temp.put("activityId",checkNullToEmpty(rs.getString("activityId")));
                String dishFoodDataStr = rs.getString("finishFoodData");
                Map<Integer, CookBoy2Dish> finishFoodMap = JSON.parseObject(dishFoodDataStr, new TypeReference<Map<Integer, CookBoy2Dish>>() {});
                for(CookBoy2Dish food : finishFoodMap.values()){
                    if(food.getStatus() == 1 && System.currentTimeMillis() > food.getTime() + 80 * 60 * 1000){
                        Map<String,String> newOne = new ConcurrentHashMap<>(temp);
                        newOne.put("dishId",""+food.getDishId());
                        newOne.put("菜品index",""+food.getIndex());
                        dataList.add(newOne);
                    }
                }
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return dataList;
    }

    private List<Map<String, String>> getLog(int activityId) {
        List<Map<String, String>> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "SELECT * FROM t_u_activity_cookboy WHERE finishFoodData like '%status\":1%' AND activityId = ?";
        try {
            call = getGameServer().getConnection().prepareCall(sql);
            int i=1;
            call.setInt(i++,activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                Map<String,String> temp = new ConcurrentHashMap<>();
                temp.put("serverId",String.valueOf(getGameServer().getServerInfo().getServerId()));
                temp.put("userId",checkNullToEmpty(rs.getString("userId")));
                temp.put("activityId",checkNullToEmpty(rs.getString("activityId")));
                String dishFoodDataStr = rs.getString("finishFoodData");
                Map<Integer, CookBoy2Dish> finishFoodMap = JSON.parseObject(dishFoodDataStr, new TypeReference<Map<Integer, CookBoy2Dish>>() {});
                for(CookBoy2Dish food : finishFoodMap.values()){
                    if(food.getStatus() == 1 && System.currentTimeMillis() > food.getTime() + 80 * 60 * 1000){
                        Map<String,String> newOne = new ConcurrentHashMap<>(temp);
                        newOne.put("dishId",""+food.getDishId());
                        newOne.put("菜品index",""+food.getIndex());
                        dataList.add(newOne);
                    }
                }
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return dataList;
    }
}
